package com.ihome.android.market2.view;

import java.util.Date;
import com.ihome.android.util.log.LogUtil;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.app.AlertDialog;
import android.webkit.WebStorage;
import android.widget.EditText;

public class EduBasicWebview extends WebView {
	private String	TAG			= "EduBasicWebview";
	private Context	mContext	= null;

	public EduBasicWebview(Context paramContext) {
		super(paramContext);
		this.mContext = paramContext;
		initWebView();
	}

	public EduBasicWebview(Context paramContext, AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);
		this.mContext = paramContext;
		initWebView();
	}

	public EduBasicWebview(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
		super(paramContext, paramAttributeSet, paramInt);
		this.mContext = paramContext;
		initWebView();
	}

	private void initWebView() {
		LogUtil.d(TAG, "initWebView start");
//		setWebChromeClient(new BasicWebChromeClient());
		setWebViewClient(new BasicWebViewClient());
		setScrollbarFadingEnabled(true);
		setScrollBarStyle(0);
		setHorizontalScrollBarEnabled(false);
		setVerticalScrollBarEnabled(false);
		setSoundEffectsEnabled(false);
		getSettings().setJavaScriptEnabled(true);
		getSettings().setPluginState(WebSettings.PluginState.ON);
		getSettings().setLoadWithOverviewMode(true);
		setInitialScale(100);
		getSettings().setUseWideViewPort(true);
		getSettings().setSupportZoom(true);
		getSettings().setBuiltInZoomControls(false);
		getSettings().setDatabaseEnabled(true);
		getSettings().setDomStorageEnabled(true);
		getSettings().setAppCacheEnabled(false);
		getSettings().setAllowFileAccess(true);
		getSettings().setNeedInitialFocus(true);
		setFocusable(true);
		int screenDensity = getResources().getDisplayMetrics().densityDpi;
		WebSettings.ZoomDensity zoomDensity = WebSettings.ZoomDensity.MEDIUM;
		switch (screenDensity) {
		case DisplayMetrics.DENSITY_LOW:
			zoomDensity = WebSettings.ZoomDensity.CLOSE;
			break;
		case DisplayMetrics.DENSITY_MEDIUM:
			zoomDensity = WebSettings.ZoomDensity.MEDIUM;
			break;
		case DisplayMetrics.DENSITY_HIGH:
			zoomDensity = WebSettings.ZoomDensity.FAR;
			break;
		}
		getSettings().setDefaultZoom(zoomDensity);
		requestFocus();
		LogUtil.d(TAG, "initWebView end");
	}

	public void doSystemOnKeyDown(int paramInt) {
		LogUtil.d(TAG, "doSystemOnKeyDown:paramInt = " + paramInt);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		int i = event.getKeyCode();
		LogUtil.d(TAG, "onKeyDown > keyCode =" + keyCode);
		if ((keyCode == KeyEvent.KEYCODE_BACK) && canGoBack()) {
			goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
			return true;
		}
		else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
			return true;
		}
		if (keyCode == KeyEvent.KEYCODE_L) {
			return super.onKeyUp(KeyEvent.KEYCODE_DPAD_LEFT, event);
		}
		if (keyCode == KeyEvent.KEYCODE_R) {
			return super.onKeyUp(KeyEvent.KEYCODE_DPAD_RIGHT, event);
		}
		return super.onKeyUp(keyCode, event);
	}

	private class BasicWebChromeClient extends WebChromeClient {

		private BasicWebChromeClient() {}

		public void onExceededDatabaseQuota(String paramString1, String paramString2, long paramLong1, long paramLong2, long paramLong3, WebStorage.QuotaUpdater paramQuotaUpdater) {
			LogUtil.d(TAG, "onExceededDatabaseQuota");
			paramQuotaUpdater.updateQuota(2L * paramLong2);
		}

		public boolean onJsAlert(WebView paramWebView, String paramString1, String paramString2, final JsResult paramJsResult) {
			AlertDialog.Builder localBuilder = new AlertDialog.Builder(EduBasicWebview.this.mContext);
			localBuilder.setTitle("提示：");
			localBuilder.setMessage(paramString2);
			localBuilder.setPositiveButton(17039370, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
					paramJsResult.confirm();
				}
			});
			localBuilder.setCancelable(false);
			localBuilder.create();
			localBuilder.show();
			return true;
		}

		public boolean onJsConfirm(WebView paramWebView, String paramString1, String paramString2, final JsResult paramJsResult) {
			AlertDialog.Builder localBuilder = new AlertDialog.Builder(EduBasicWebview.this.mContext);
			localBuilder.setTitle("提示：");
			localBuilder.setMessage(paramString2);
			localBuilder.setPositiveButton(17039370, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
					paramJsResult.confirm();
				}
			});
			localBuilder.setNegativeButton(17039360, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
					paramJsResult.cancel();
				}
			});
			localBuilder.setCancelable(false);
			localBuilder.create();
			localBuilder.show();
			return true;
		}

		public boolean onJsPrompt(WebView paramWebView, String paramString1, String paramString2, String paramString3, final JsPromptResult paramJsPromptResult) {
			AlertDialog.Builder localBuilder = new AlertDialog.Builder(EduBasicWebview.this.mContext);
			localBuilder.setTitle("提示");
			final EditText localEditText = new EditText(paramWebView.getContext());
			localEditText.setSingleLine();
			localEditText.setText(paramString3);
			localBuilder.setView(localEditText);
			localBuilder.setMessage(paramString2);
			localBuilder.setPositiveButton(17039370, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
					paramJsPromptResult.confirm(localEditText.getText().toString());
				}
			}).setNeutralButton(17039360, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
					paramJsPromptResult.cancel();
				}
			});
			localBuilder.setCancelable(false);
			localBuilder.create();
			localBuilder.show();
			return true;
		}

		public void onProgressChanged(WebView paramWebView, int paramInt) {
			LogUtil.d(TAG, "onProgressChanged");
		}

		public void onReachedMaxAppCacheSize(long paramLong1, long paramLong2, WebStorage.QuotaUpdater paramQuotaUpdater) {
			LogUtil.d(TAG, "onReachedMaxAppCacheSize");
			paramQuotaUpdater.updateQuota(2L * paramLong1);
		}
	}

	private class BasicWebViewClient extends WebViewClient {
		private BasicWebViewClient() {}

		public void onPageFinished(WebView paramWebView, String paramString) {
			LogUtil.d(TAG, "onPageFinished:" + new Date().getTime());
			paramWebView.setFocusable(false);
			super.onPageFinished(paramWebView, paramString);
		}

		public void onPageStarted(WebView paramWebView, String paramString, Bitmap paramBitmap) {
			LogUtil.d(TAG, "onPageStarted:" + paramString);
			super.onPageStarted(paramWebView, paramString, paramBitmap);
		}

		public void onReceivedError(WebView paramWebView, int paramInt, String paramString1, String paramString2) {
			LogUtil.d(TAG, "onReceivedError");

			shouldOverrideUrlLoading(paramWebView, paramString2);
		}

		public boolean shouldOverrideUrlLoading(WebView paramWebView, String paramString) {

			LogUtil.d(TAG, "shouldOverrideUrlLoading:" + paramString);
			return false;

		}
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (inputMethodManager.isActive() == false) {
			int i = event.getKeyCode();
			loadUrl("javascript:(function d(el, type) {var evt = document.createEvent('Event');evt.initEvent(type,true,true);evt.keyCode = " + i + ";el.dispatchEvent(evt);} )(document, 'keydown');");
		}

		return super.dispatchKeyEvent(event);
	}

}
